const conn = require('../app/database')

class CategoryModel {

  async getCategoryList() {
    const statement = `
      SELECT id, category_name categoryName
      FROM goods_category;
    `

    const [result] = await conn.execute(statement)
    return result
  }

  async getCategoryDetali(categoryId) {
    const statement = `
      select gc.id categoryId, gc.category_name categoryName,
        IF(g.spu_no,
        JSON_ARRAYAGG(
              JSON_OBJECT('goodsId', g.id, 'spuNo', g.spu_no, 'goodsName', g.goods_name, 'showImg', g.show_img, 'initialPrice', g.initial_price, 'newPrice', new_price, 'categoryId', category_id, 'themeId', g.theme_id, 'goodsDesc', g.goods_desc)
            ) 
        , NULL) goodsList
      FROM goods_category gc 
      LEFT JOIN goods g ON gc.id = g.category_id
      WHERE gc.id = ?
      GROUP BY gc.id;
    `

    const [result] = await conn.execute(statement, [categoryId])
    return result
  }

}


module.exports = new CategoryModel()

